﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class GiftFlowerDAO : BaseService
    {
        public GiftFlowerDAO() { }
        public int GetMaxId()
        {
            string hql = "from GiftFlower order by Id desc";
            try
            {
                GiftFlower prod = FindList<GiftFlower>(hql, null, 0, 1).First();
                return prod.Id + 1;
            }
            catch (Exception)
            {

                return 1;
            }
        }
        public IList<GiftFlower> GetListByPager(int page, int pagesize, int listid)
        {
            string hql = " from GiftFlower where 1=1";
            if (listid > 0)
            {
                hql += " and ListId=" + listid;
            }
            hql += " order by isTejia Desc, OrderNum Desc";
            try
            {
                if (page == -1)
                {
                    return FindList<GiftFlower>(hql, null, null, null);
                }
                else
                {
                    return FindList<GiftFlower>(hql, null, (page - 1) * pagesize, pagesize);
                }
            }
            catch (Exception)
            {

                return null;
            }

        }

        public int GetListByPager(int listid)
        {
            string hql = "select count(*) from GiftFlower where 1=1";
            if (listid > 0)
            {
                hql += " and ListId=" + listid;
            }
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {
                return 0;
            }

        }
        public IList<GiftFlower> GetList(int page, int pagesize, int istejia, string num, int minp, int maxd, string keys, string cate)
        {
            string hql = " from GiftFlower where 1=1";
            if (istejia > -1)
            {
                hql += " and IsTejia=" + istejia;
            }
            if (!string.IsNullOrEmpty(num))
            {
                hql += " and FlowerNum= '" + num+"'";
            }
            if (minp > 0)
            {
                hql += " and FlowerPrice>=" + minp;
            }
            if (maxd > 0)
            {
                hql += " and FlowerPrice<=" + maxd;
            }
            if (!string.IsNullOrEmpty(keys))
            {
                hql += " and ( Keys like '%" + keys + "%'";
                hql += " or FlowerName like '%" + keys + "%' )";
            }
            if (!string.IsNullOrEmpty(cate))
            {
                hql += " and Cate like '%" + cate + "%'";
            }
            hql += " order by isTejia Desc, OrderNum Desc";
            try
            {
                if (page == -1)
                {
                    return FindList<GiftFlower>(hql, null, null, null);
                }
                else
                {
                    return FindList<GiftFlower>(hql, null, (page - 1) * pagesize, pagesize);
                }
            }
            catch (Exception)
            {

                return null;
            }

        }
        public int GetList(int istejia, string num, int minp, int maxd, string keys, string cate)
        {
            string hql = "select count(*) from GiftFlower where 1=1";
            if (istejia > -1)
            {
                hql += " and IsTejia=" + istejia;
            }
            if (!string.IsNullOrEmpty(num))
            {
                hql += " and FlowerNum= '" + num + "'";
            }
            if (minp > 0)
            {
                hql += " and FlowerPrice>=" + minp;
            }
            if (maxd > 0)
            {
                hql += " and FlowerPrice<=" + maxd;
            }
            if (!string.IsNullOrEmpty(keys))
            {
                hql += " and Keys like '%" + keys + "%'";
            }
            if (!string.IsNullOrEmpty(cate))
            {
                hql += " and Cate like '%" + cate + "%'";
            }
            
            try
            {


                return Convert.ToInt32(GetValue(hql));
                
            }
            catch (Exception)
            {

                return 0;
            }

        }

    }
}
